package cn.jekun.shop.app.action;


import java.sql.Types;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.core.bean.gen.McMember;
import cn.jekun.component.core.service.gen.McMemberService;
import cn.jekun.component.shop.action.ProvPayAction;
import cn.jekun.shop.common.service.ShopRecGoodsService;
import net.sf.json.JSONObject;


@SuppressWarnings("serial")
public class ShopRecGoodsAction extends AppBaseAction{	

	@Resource(name = ShopRecGoodsService.BEAN_NAME)
	ShopRecGoodsService shopRecGoodsService;
	@Resource(name = McMemberService.BEAN_NAME)
	McMemberService memberService;
    //记录日志信息
    private static Logger logger=Logger.getLogger(ShopRecGoodsAction.class);
	/**
	 * 商家信息及推荐商品
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getShopRecGoods(){

		JSONObject jsonObject = null;
		try {
			String shopId = request.getParameter("shopId");
			String userId = request.getParameter("userId");
			this.validateSign(shopId); //验证签名
			jsonObject = this.shopRecGoodsService.getShopRecGoods(shopId,userId);

			if(StringUtils.isNotBlank(userId) && jsonObject.get("shop") != null){
				formMap.put(McMember.COL_SHOP_ID, shopId);
				formMap.put(McMember.COL_USER_ID, userId);
				formMap.put(McMember.COL_CONCERN, "1");
				SqlUtis.prepareSql(formMap, 
						SqlUtis.getSQL(formMap, McMember.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ),
						SqlUtis.getSQL(formMap, McMember.COL_CONCERN,Types.VARCHAR,SqlUtis.EQ)
						,SqlUtis.getSQL(formMap, McMember.COL_USER_ID,Types.VARCHAR,SqlUtis.EQ));
				JSONObject json = (JSONObject)jsonObject.get("shop");
				if(this.memberService.exists(formMap)){
					json.put("memberflag", "1");
				}else{
					json.put("memberflag", "0");
				}
			}
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);

	}
	
	/**
	 * 
	* @Title: scoreShop
	* @Description: 用户对商家的评分
	* @param @return    设定文件
	* @return String    返回类型
	* @throws
	 */
	@SuppressWarnings("unchecked")
	public String scoreShop(){
	    JSONObject jsonObject = null;
	    try{
	    	logger.info("************************************ShopRecGoodsAction scoreShop**************************");
	    	String userId = request.getParameter("userId");
	    	String shopId = request.getParameter("shopId");
	    	String score = request.getParameter("score");
            //登录验证
	    	this.validateLoginApp();
	    	this.validateSign(this.getLoginSign(),userId,shopId,score);
	    	String source_txt = "********userId="+userId+"********shopId="+shopId+"********score="+score;
	    	logger.info("*********************************参数"+source_txt+"******************************************");
	    	this.shopRecGoodsService.updateScore(shopId, userId, score);	    	
	    	jsonObject = this.shopRecGoodsService.addScoreHis(shopId, userId, score);
	    }catch(BusinessException e){
	    	jsonObject = JSONObject.fromObject(e.getErrorMsg());
	    }catch(Exception e){
	    	jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
	    }
		return this.writeJson(jsonObject);
	}

	/**
	 * 
	* @Title: getScoreHis
	* @Description: 获取用户上次评分的记录
	* @param @return    设定文件
	* @return String    返回类型
	* @throws
	 */
	public String  getScoreHis() {
		JSONObject jsonObject = null;
		try{
	    	logger.info("************************************ShopRecGoodsAction getScoreHis**************************");
	    	String userId = request.getParameter("userId");
	    	String shopId = request.getParameter("shopId");
	    	String source_txt = "********userId="+userId+"********shopId="+shopId+"********";
	    	this.validateLoginApp();
	    	this.validateSign(this.getLoginSign(),userId,shopId);
	    	jsonObject = this.shopRecGoodsService.getScoreHis(userId,shopId);
	    	logger.info("************************************"+ jsonObject +"**************************");
		}catch(BusinessException e){
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		}catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
		}
		return this.writeJson(jsonObject);
	}
}
